import time
import uuid
from io import StringIO

import numpy as np
import pandas as pd

from utils import SignGenUtil
import requests


def get_1991_2020_temp_data(station, time_range):
    serviceNodeId = 'NMIC_MUSIC_CMADAAS'
    serviceIp = '10.69.90.120'
    interfaceId = 'getSurfMMonEleByMonthsOfYearAndStaID'
    dataCode = 'SURF_CLI_CHN_MMON_TEM'
    elements = 'Station_Id_C,Mon_Seq,TEM_Avg'
    staIds = station
    monsOfYear = time_range
    userId = 'ZHZX_FYZX_BZK'
    pwd = 'Fyzxjsbzk_4682'
    dataFormat = 'json'
    timestamp = str(int(round(time.time() * 1000)))
    nonce = str(uuid.uuid1())
    signParams = {'serviceNodeId': serviceNodeId,
                  'userId': 'ZHZX_FYZX_BZK',
                  'pwd': pwd,
                  'interfaceId': interfaceId,
                  'dataCode': dataCode,
                  'elements': elements,
                  'staIds': staIds,
                  'monsOfYear': monsOfYear,
                  'dataFormat': dataFormat,
                  'timestamp': timestamp,
                  'nonce': nonce,

                  }
    signUtil = SignGenUtil.SignGenUtil()
    sign = signUtil.getSign(signParams)
    if sign == "":
        print("generate sign is None")
    baseUrl = 'http://' + serviceIp + '/music-ws/api?serviceNodeId=' + serviceNodeId + \
              '&userId=' + userId + \
              '&interfaceId=' + interfaceId + \
              '&dataCode=' + dataCode + \
              '&elements=' + elements + \
              '&staIds=' + staIds + \
              '&monsOfYear=' + monsOfYear + \
              '&dataFormat=' + dataFormat + \
              '&timestamp=' + timestamp + \
              '&nonce=' + nonce + \
              '&sign=' + sign

    response = requests.get(baseUrl)
    # 检查请求是否成功
    if response.status_code == 200:
        csv_data = response.json()
        # 读取CSV数据

        return csv_data
    else:
        return "Failed to retrieve data:", response.status_code


def get_1991_2020_rain_data(station, time_range):
    serviceNodeId = 'NMIC_MUSIC_CMADAAS'
    serviceIp = '10.69.90.120'
    interfaceId = 'getSurfMMonEleByMonthsOfYearAndStaID'
    dataCode = 'SURF_CLI_CHN_MMON_PRE'
    elements = 'Station_Id_C,Mon_Seq,PRE'
    staIds = station
    monsOfYear = time_range
    userId = 'ZHZX_FYZX_BZK'
    pwd = 'Fyzxjsbzk_4682'
    dataFormat = 'json'
    timestamp = str(int(round(time.time() * 1000)))
    nonce = str(uuid.uuid1())
    signParams = {'serviceNodeId': serviceNodeId,
                  'userId': 'ZHZX_FYZX_BZK',
                  'interfaceId': interfaceId,
                  'dataCode': dataCode,
                  'elements': elements,
                  'staIds': staIds,
                  'monsOfYear': monsOfYear,
                  'dataFormat': dataFormat,
                  'timestamp': timestamp,
                  'nonce': nonce,
                  'pwd': pwd,
                  }
    signUtil = SignGenUtil.SignGenUtil()
    sign = signUtil.getSign(signParams)
    if (sign == ""):
        print("generate sign is None")
    baseUrl = 'http://' + serviceIp + '/music-ws/api?serviceNodeId=' + serviceNodeId + \
              '&userId=' + userId + \
              '&interfaceId=' + interfaceId + \
              '&dataCode=' + dataCode + \
              '&elements=' + elements + \
              '&staIds=' + staIds + \
              '&monsOfYear=' + monsOfYear + \
              '&dataFormat=' + dataFormat + \
              '&timestamp=' + timestamp + \
              '&nonce=' + nonce + \
              '&sign=' + sign

    response = requests.get(baseUrl)

    # 检查请求是否成功
    if response.status_code == 200:
        csv_data = response.json()
        # 读取CSV数据
        return csv_data

    else:
        return "Failed to retrieve data:", response.status_code
